import apis from '../../http/apis';

export default {
    namespaced: true,
    state:{
        customers:[],
        allCustomers: [],
        pageData:{
            onePageData: '3',
            nowPage: 1,
            pages: '',
            total: 0
        }
    },
    mutations: {
        changeCustomers(state,data){
            state.customers = data.data;
             
        },
        changeAllCustomers(state,data){
            state.allCustomers = data.data;
            state.pageData.total = data.total;
            state.pages = data.pages
        },
        changeOnePageData(state,val){
            state.pageData.onePageData = val
        }
    },
    actions: {
        //新增用户
        async customersAdd(context,customer){
            const data = await apis.customers.customersAdd(customer);
            if(data.data.status){
                alert('添加成功')
            }
        },
        //通过销售员Id获取用户
        async getCustomersBySalesmansId(context,_id){
            
            const data =await apis.customers.getCustomersBySalesmansId(_id);
            console.log(data);
            if(data.data.status){
                context.commit('changeCustomers',data.data) 
            }
        },
        //获取所有用户
        async getCustomers(context,searchData){
            const data = await apis.customers.getCustomers({
                onePageData: context.state.pageData.onePageData,
                nowPage: context.state.pageData.nowPage,
                ...searchData
            });
            if(data.data.status){
                context.commit('changeAllCustomers',data.data.data)
            }
        },
        //通过_id删除用户
        async deleteCustomers(context,_id){
            const data = await apis.customers.deleteCustomers({_id});
            if(data.data.status){
                alert('删除成功');
            }
        },
        //通过id获取一个用户
        // async getOeCustomer(context,_id){
        //     const data = await apis.customers.getOneCustomer({_id});
        //     if(data.data.length>0){
        //         context.commit('changeCustomer',data.data[0])
        //     }
        // }
        //修改用户数据
        async sureAlterCustomers(context,customer){
            const data = await apis.customers.alterCustomer(customer);
            if(data.data.status){
                alert('修改成功')
            }
        }
    },
}